package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;


import com.itheima.reggie.domain.Address;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class AddressController {

    @Autowired
    private AddressService addressService;

    //收货地址
    @GetMapping("/address/list")
    public ResultInfo list(){
        //调用service
        List<Address>addressList = addressService.list();

        //返回结果
        return ResultInfo.success(addressList);
    }

    //新增地址
    @PostMapping("/address")
    public ResultInfo save(@RequestBody Address address){//接收请求参数
        //调用service
        addressService.save(address);

        //返回结果
        return ResultInfo.success();
    }

    //设置默认地址
    @PutMapping("/address/default")
    public ResultInfo setDefault(@RequestBody Map<String,Long> param){
        //1.接受请求参数
        Long id = param.get("id");
        //调用service
        addressService.setDefault(id);

        //返回结果
        return ResultInfo.success();
    }

    // 去结算（查询默认收货地址
    @GetMapping("/address/default")
    public ResultInfo findDefault(){
        //调用service
        Address addressList = addressService.findDefault();

        //返回结果
        return ResultInfo.success(addressList);
    }

    ////地址回显
    @GetMapping("/address/{id}")
    public ResultInfo findByIdAddress(@PathVariable Long id){
        //调用service
         Address addressList = addressService.findByIdAddress(id);

        //返回结果
        return ResultInfo.success(addressList);
    }

    //修改地址
    @PutMapping("/address")
    public ResultInfo updateByAddress(@RequestBody Address address){
        //调用service
        addressService.updateByAddress(address);

        //返回结果
        return ResultInfo.success();
    }

    //删除地址
    @DeleteMapping("/address")
    public ResultInfo deleteByAddress(Long ids){
        //调用service
        addressService.deleteByAddress(ids);

        //返回结果
        return ResultInfo.success();
    }
}

